package com.hd123.rumba.gwt.widget.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.hd123.rumba.gwt.widget.client.grid.DxGrid;
import com.hd123.rumba.gwt.widget.client.grid.DxGridColumnDef;
import com.hd123.rumba.gwt.widget.client.grid.SinglePageDataProvider;
import com.hd123.rumba.gwt.widget.client.grid.StringEditor;
import com.hd123.rumba.gwt.widget.client.grid.StringRendererFactory;

public class Widgets implements EntryPoint {

  DxGrid readOnlyGrid, editableGrid;

  public void onModuleLoad() {
    RootPanel.get("DxGrid-ReadOnly").add(readOnlyGrid = createReadOnlyGrid());
    readOnlyGrid.refresh();
    RootPanel.get("DxGrid-Editable").add(editableGrid = createEditableGrid());
    editableGrid.refresh();
  }

  public DxGrid createReadOnlyGrid() {
    DxGrid grid = new DxGrid(1, 3);
    grid.setWidth("100%");
    grid.setProvider(new SinglePageDataProvider() {
      public int getRowCount() {
        return 10;
      }

      public int getColumnCount() {
        return 3;
      }

      public String getData(int row, int col) {
        if (row >= 0 && row < getRowCount()) {
          return "(" + row + ")" + col;
        } else
          return "";
      }

    });

    DxGridColumnDef colDef;
    StringRendererFactory srf;

    grid.addColumnDef(colDef = new DxGridColumnDef("Left String"));
    colDef.setRendererFactory(srf = new StringRendererFactory());

    grid.addColumnDef(colDef = new DxGridColumnDef("Center String"));
    colDef.setRendererFactory(srf = new StringRendererFactory());
    srf.setHorizontalAlignment(StringRendererFactory.ALIGN_CENTER);

    grid.addColumnDef(colDef = new DxGridColumnDef("Right String"));
    colDef.setRendererFactory(srf = new StringRendererFactory());
    srf.setHorizontalAlignment(StringRendererFactory.ALIGN_RIGHT);

    return grid;
  }

  public DxGrid createEditableGrid() {
    DxGrid grid = new DxGrid(1, 3);
    grid.setWidth("100%");
    grid.setProvider(new SinglePageDataProvider() {
      public int getRowCount() {
        return 10;
      }

      public int getColumnCount() {
        return 3;
      }

      public String getData(int row, int col) {
        if (row >= 0 && row < getRowCount()) {
          return "(" + row + ")" + col;
        } else
          return "";
      }

    });

    grid.setReadOnly(false);

    DxGridColumnDef colDef;
    StringRendererFactory srf;

    grid.addColumnDef(colDef = new DxGridColumnDef("Left String"));
    colDef.setRendererFactory(srf = new StringRendererFactory());
    colDef.setEditor(new StringEditor());
    colDef.setReadOnly(false);
    colDef.setEnterStop(true);

    grid.addColumnDef(colDef = new DxGridColumnDef("Center String"));
    colDef.setRendererFactory(srf = new StringRendererFactory());
    srf.setHorizontalAlignment(StringRendererFactory.ALIGN_CENTER);
    colDef.setEditor(new StringEditor());
    colDef.setReadOnly(false);
    colDef.setEnterStop(false);

    grid.addColumnDef(colDef = new DxGridColumnDef("Right String"));
    colDef.setRendererFactory(srf = new StringRendererFactory());
    srf.setHorizontalAlignment(StringRendererFactory.ALIGN_RIGHT);
    colDef.setEditor(new StringEditor());
    colDef.setReadOnly(false);
    colDef.setEnterStop(true);

    return grid;
  }

}
